package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.LoginService;
import com.tarena.lbs.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理和后台用户相关的业务接口
 */

@Slf4j
@RestController
@Tag(name = "后台用户接口", description = "处理和后台用户相关的业务接口")
public class AdminController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private AdminService adminService;


    @Operation(summary = "管理员登录")
    @PostMapping("/passport/admin/login")
    public Result<LoginVO> adminLogin(AdminLoginParam param) throws BusinessException {
        log.debug("管理员登录业务处理,入参:{}",param);
        LoginVO vo = loginService.doLogin(param);
        log.debug("管理员登录成功，入参:{},出参:{}",param,vo);
        return new Result<>(vo);
    }


    @Operation(summary = "用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail() throws BusinessException {
        log.debug("查询登录用户详情");
        AdminVO vo = adminService.detail();
        log.debug("查询登录用户详情成功，出参:{}",vo);
        return new Result<>(vo);
    }


    /**
     * 查询后台管理的 账号列表
     */
    @Operation(summary = "账号列表")
    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> pageList(@RequestBody AdminQuery query)
        throws BusinessException{
        PageResult<AdminVO> voPage = adminService.pageList(query);
        return new Result<>(voPage);
    }

    @Operation(summary = "账号保存")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> save(@RequestBody AdminParam param)
        throws BusinessException{
        log.debug("保存账号业务处理,入参:{}",param);
        adminService.save(param);
        return Result.success();
    }
    @Operation(summary = "删除账号")
    @DeleteMapping("/admin/basic/role/delete")
    public Result<Void> delete(@RequestParam("id") Integer id)
            throws BusinessException{
        log.info("删除账号，入参:{}",id);
        adminService.delete(id);
        return Result.success();
    }
    @Operation(summary = "重置密码")
    @PutMapping("/admin/basic/role/resetPassword")
    public Result<Void> resetPassword(@RequestParam("id") Integer id)
            throws BusinessException{
        log.info("重置密码，入参:{}",id);
        adminService.resetPassword(id);
        return Result.success();
    }
    @Operation(summary = "修改账号状态")
    @PutMapping("/admin/basic/role/updateStatus")
    public Result<Void> updateStatus(@RequestBody AdminParam param)
            throws BusinessException{
        log.info("修改账号状态，入参:{}",param);
        adminService.updateStatus(param);
        return Result.success();
    }
    @Operation(summary = "编辑账号")
    @PutMapping("/admin/basic/role/update")
    public Result<Void> update(@RequestBody AdminParam param)
            throws BusinessException{
        log.info("编辑账号，入参:{}",param);
        if (param == null || param.getId() == null) {
            log.error("缺少必要的ID参数");
            throw new IllegalArgumentException("更新失败：必须提供有效的ID参数");
        }
        adminService.updateById(param);
        return Result.success();
    }
    
    @Operation(summary = "账号查询")
    @PostMapping("/admin/basic/role/query")
    public Result<PageResult<AdminVO>> query(@RequestBody AdminQuery query)
            throws BusinessException{
        log.info("账号查询，入参:{}",query);
        PageResult<AdminVO> voPage = adminService.pageList(query);
        return new Result<>(voPage);
    }


    /**
     * 查询用户列表
     */
    @Operation(summary = "用户列表查询")
    @PostMapping("/admin/basic/user/list")
    public Result<PageResult<UserVO>> pageList(@RequestBody UserQuery query){
        return adminService.userList(query);
    }

    /*
    * 查询用户详情
    * */
    @Operation(summary = "用户详情查询")
    @GetMapping("/admin/basic/user/detail/{id}")
    public Result<UserVO> detail(@PathVariable Integer id){
        return adminService.selectUserById(id);
    }









































}